package com.xiliu.rocketmq.transaction;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author xiliu 事物消息生产者
 * @description
 * @date 2022/3/23
 */
public class TransactionProducer {

    public static void main(String[] args) throws Exception{
        TransactionMQProducer producer = new TransactionMQProducer("xiliu_producer_group");
        producer.setNamesrvAddr("42.194.222.32:9876");
        // 设置超时时间
        producer.setSendMsgTimeout(8000);
        // 设置事物监听器
        producer.setTransactionListener(new TransactionListenerImpl());
        producer.start();

        // 发消息
        Message message = new Message("broker-a","transaction_msg","用户A给用户B转账500元".getBytes(RemotingHelper.DEFAULT_CHARSET));
        SendResult sendResult = producer.sendMessageInTransaction(message,null);

        System.out.printf("%s%n", sendResult);
        producer.shutdown();
    }
}
